package org.jeecg.modules.zcgl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgBaseController;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.zcgl.entity.Region;
import org.jeecg.modules.zcgl.service.IRegionService;
import org.jeecg.modules.zcgl.vo.RegionIdModel;
import org.jeecg.modules.zcgl.vo.RegionTreeModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 区域信息
 * @Author: jeecg-boot
 * @Date: 2020-08-11
 * @Version: V1.0
 */
@Api(tags = "区域信息")
@RestController
@RequestMapping("/zcgl/region")
@Slf4j
public class RegionController extends JeecgBaseController<Region, Region, IRegionService> {
    @Autowired
    private IRegionService regionService;

    @Autowired
    public RedisTemplate<String, Object> redisTemplate;

    /**
     * 查询数据 查出我的区域,并以树结构数据格式响应给前端
     *
     * @return
     */
    @RequestMapping(value = "/queryMyRegionTreeList", method = RequestMethod.GET)
    public Result<List<RegionTreeModel>> queryMyRegionTreeList() {
        List<RegionTreeModel> list = regionService.queryMyRegionTreeList();
        return Result.ok(list);
    }

    /**
     * 查询数据 查出所有区域,并以树结构数据格式响应给前端
     *
     * @return
     */
    @RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)
    public Result<List<RegionTreeModel>> queryTreeList() {
        List<RegionTreeModel> list = regionService.queryTreeList();
        return Result.ok(list);
    }

    /**
     * 添加新数据 添加用户新建的区域对象数据,并保存到数据库
     *
     * @param region
     * @return
     */
    //@RequiresRoles({"admin"})
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @CacheEvict(value= {CacheConstant.ZCGL_REGION_IDS_CACHE, CacheConstant.ZCGL_REGION_CACHE, "'regex:'" + CacheConstant.ZCGL_MY_REGION_CACHE + "'*'"}, allEntries=true)
    public Result<Region> add(@RequestBody Region region, HttpServletRequest request) {
        regionService.saveRegionData(region);
        return Result.ok();
    }

    /**
     * 编辑数据 编辑区域的部分数据,并保存到数据库
     *
     * @param region
     * @return
     */
    //@RequiresRoles({"admin"})
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @CacheEvict(value = {CacheConstant.ZCGL_REGION_IDS_CACHE, CacheConstant.ZCGL_REGION_CACHE, "'regex:'" + CacheConstant.ZCGL_MY_REGION_CACHE + "'*'"}, allEntries = true)
    public Result<Region> edit(@RequestBody Region region, HttpServletRequest request) {
		boolean ok = regionService.updateRegionDataById(region);
        return Result.ok();
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    //@RequiresRoles({"admin"})
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @CacheEvict(value = {CacheConstant.ZCGL_REGION_IDS_CACHE, CacheConstant.ZCGL_REGION_CACHE, "'regex:'" + CacheConstant.ZCGL_MY_REGION_CACHE + "'*'"}, allEntries = true)
    public Result<Region> delete(@RequestParam(name = "id", required = true) String id) {

        boolean ok = regionService.delete(id);
        return Result.ok();
    }


    /**
     * 批量删除 根据前端请求的多个ID,对数据库执行删除相关区域数据的操作
     *
     * @param ids
     * @return
     */
    //@RequiresRoles({"admin"})
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    @CacheEvict(value= {CacheConstant.ZCGL_REGION_IDS_CACHE, CacheConstant.ZCGL_REGION_CACHE, "'regex:'" + CacheConstant.ZCGL_MY_REGION_CACHE + "'*'"}, allEntries=true)
    public Result<Region> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {

        if (ids == null || "".equals(ids.trim())) {
            return Result.error("参数不识别！");
        } else {
            this.regionService.deleteBatchWithChildren(Arrays.asList(ids.split(CommonConstant.DEFAULT_SEPARATOR)));
        }
        return Result.ok();
    }

    /**
     * 查询数据 添加或编辑页面对该方法发起请求,以树结构形式加载所有区域的名称,方便用户的操作
     *
     * @return
     */
    @RequestMapping(value = "/queryIdTree", method = RequestMethod.GET)
    public Result<List<RegionIdModel>> queryIdTree() {
        List<RegionIdModel> list = regionService.queryRegionIdTreeList();
        return Result.ok(list);
    }

    /**
     * <p>
     * 区域搜索功能方法,根据关键字模糊搜索相关区域
     * </p>
     *
     * @param keyWord
     * @return
     */
    @RequestMapping(value = "/searchBy", method = RequestMethod.GET)
    public Result<List<RegionTreeModel>> searchBy(@RequestParam(name = "keyWord", required = true) String keyWord) {
        Map<String, Object> par = new HashMap<>();
        par.put("keyWord", keyWord);
        List<RegionTreeModel> list = regionService.queryMyRegionTreeList(par);
        return Result.ok(list);
    }


    /**
     * 导出excel
     *
     * @param request
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(Region region, HttpServletRequest request) {
        return super.exportXls(request, region, Region.class, "区域信息");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresRoles({"admin"})
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    @CacheEvict(value= {CacheConstant.ZCGL_REGION_IDS_CACHE, CacheConstant.ZCGL_REGION_CACHE, "'regex:'" + CacheConstant.ZCGL_MY_REGION_CACHE + "'*'"}, allEntries=true)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Region.class);
    }


    /**
     * 查询所有区域信息
     *
     * @return
     */
    @GetMapping("listAll")
    public Result<List<Region>> listAll(@RequestParam(name = "id", required = false) String id) {
        LambdaQueryWrapper<Region> query = new LambdaQueryWrapper<Region>();
//		 query.orderByAsc(Region::getOrgCode);
        if (oConvertUtils.isNotEmpty(id)) {
            String arr[] = id.split(CommonConstant.DEFAULT_SEPARATOR);
            query.in(Region::getId, arr);
        }
        List<Region> ls = this.regionService.list(query);
        return Result.ok(ls);
    }

    @Override
    public List<Region> exportList(List<Region> list) {
        return list;
    }

    @Override
    public boolean importSave(Region region) {
        return regionService.saveRegionData(region);
    }
}
